package com.mes.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.Query;
import org.openbravo.dal.service.OBDal;
import org.openbravo.dal.service.OBQuery;
import org.openbravo.model.common.plm.Product;
import org.openbravo.model.common.plm.ProductCategory;
import org.openbravo.model.common.uom.UOM;

@SuppressWarnings("unchecked")
public class CommonUtils {
	/**
	 * @param regex 正则表达式字符串
	 * @param str 要匹配的字符串
	 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
	 */
	private static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	/**
	 * 根据物料编码查询物料
	 * @param code
	 * @return
	 */
	public static Product getProduct(String code) {
		String whereClause = "as mp where mp.searchKey = :value";
		OBQuery<Product> query = OBDal.getInstance().createQuery(Product.class, whereClause);
		query.setNamedParameter("value", code);
		Product product = (Product) query.uniqueResult();
		return product;
	}
	/**
	 * 初始化物料类别
	 * @param code
	 * @return
	 */
	public static Map getProductCategory() {
		List<ProductCategory> pcList = new ArrayList<ProductCategory>();
		Map<String, ProductCategory> pcMap = new HashMap<String, ProductCategory>();
		String sql = "select * from M_Product_Category";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql).addEntity(ProductCategory.ENTITY_NAME);
		pcList = query.list();
		for (ProductCategory o : pcList) {
			pcMap.put(o.getName().toLowerCase(), o);
		}
		return pcMap;
	}
	/**
	 * 初始化单位
	 * @param code
	 * @return
	 */
	public static Map<String, UOM> getUOM() {
		List<UOM> uomList = new ArrayList<UOM>();
		Map<String, UOM> uomMap = new HashMap<String, UOM>();
		String sql = "select * from C_UOM";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql).addEntity(UOM.ENTITY_NAME);
		uomList = query.list();
		for (UOM o : uomList) {
			uomMap.put(o.getEDICode().toLowerCase(), o);
		}
		return uomMap;
	}
	/**
	 * 验证输入正数
	 * 
	 * @param 待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean IsDecimal(String str) {
		//	String regex = "^[1-9]\\d*(\\.\\d+)?";
		String regex = "^([1-9][0-9]{0,}|[0-9]{1,}.[0-9]{0,})$";
		//	String regex = "^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$";
		return match(regex, str);
	}
	/**
	 * 验证日期时间
	 * 
	 * @param 待验证的字符串
	 * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isDate(String str) {
		// 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],
		// [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])
		// 没加时间验证的YYYY-MM-DD
		String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|" +
				"(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|" +
				"(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|" +
				"(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|" +
				"((16|[2468][048]|[3579][26])00))-0?2-29-))$";
		return match(regex, str);
	}
	/** 
	 * 判断是否为合法IP 
	 * @return the ip 
	 */
	public static boolean isboolIp(String ipAddress) {
		String ip = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
		Pattern pattern = Pattern.compile(ip);
		Matcher matcher = pattern.matcher(ipAddress);
		return matcher.matches();
	}
}
